package br.com.dojo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.com.dojo.bean.Carro;
import br.com.dojo.dao.CarroDAO;

@Service
@Transactional
public class CarroService {

	@Autowired
	CarroDAO dao;
	
	public List<Carro> listarCarro(){
		
		List<br.com.dojo.entity.Carro> carros = dao.findAll();
		
		List<Carro> result = new ArrayList<Carro>();
		for (br.com.dojo.entity.Carro carro : carros) {
			Carro c = new Carro();
			c.setPlaca(carro.getPlaca());
			c.setAno(carro.getAno());
			c.setMarca(carro.getMarca());
			c.setQuilometragem(carro.getQuilometragem());
			c.setPatrimonio(carro.getPatrimonio());			
			result.add(c);
		}		
		
		return result;
	}
	
	public void salvar(Carro carro) {
		br.com.dojo.entity.Carro c = new br.com.dojo.entity.Carro();
		c.setPlaca(carro.getPlaca());
		c.setAno(carro.getAno());
		c.setMarca(carro.getMarca());
		c.setQuilometragem(carro.getQuilometragem());
		c.setPatrimonio(carro.getPatrimonio());	
		
		dao.save(c);
	}
	
}
